package com.hwl.eduservice.controller.admin;


import com.hwl.commonutils.result.RespBean;
import com.hwl.eduservice.pojo.Subject;
import com.hwl.eduservice.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-09-21
 */
@Api(tags = "课程分类管理")
@RestController
@RequestMapping("/admin/eduSubject")
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    @ApiOperation("批量导入课程分类")
    @PostMapping("import")
    public RespBean batchImport(MultipartFile file) {
        subjectService.batchImport(file);
        return RespBean.ok().message("批量导入课程分类成功！");
    }

    @ApiOperation("获取所有的课程分类")
    @GetMapping("getAllSubject")
    public RespBean getAllSubject() {
        List<Subject> result = new ArrayList<>();

        List<Subject> oneSubject = subjectService.getOneSubject();
        for (Subject subject : oneSubject) {
            subject.setChildren(subjectService.getTwoSubject(subject.getId()));
            result.add(subject);
        }
        return RespBean.ok().data(result);
    }

}

